package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.entity.cggl.Cgjsdmx;
import com.hnkywl.sts.entity.cggl.Xsjsdmx;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Jerry on 13-10-16.
 */
@Repository
public class XsjsdmxDao extends SimpleHibernateTemplate<Xsjsdmx, Long> {
    public List<Map> findXsjsdmxSearch(Long xsjsdId) {
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("gg", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsd", "xsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "khcd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jsd", "xsjsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsmx", "xsmx", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("xsd.id").as("xsd.id"))
                                .add(Property.forName("xsd.xsdh").as("xsdh"))
                                .add(Property.forName("ck").as("ck"))
                                .add(Property.forName("wzgg.id").as("wzgg.id"))
                                .add(Property.forName("wzgg.code").as("wzgg.code"))
                                .add(Property.forName("wzgg.name").as("wzgg.name"))
                                .add(Property.forName("wzgg.cz").as("wzgg.cz"))
                                .add(Projections.sqlProjection("wzgg1_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                                .add(Property.forName("cksl").as("ck_sbsl"))
                                .add(Property.forName("khcd.id").as("cd.id"))
                                .add(Property.forName("khcd.z").as("cd.z"))
                                .add(Property.forName("hsj").as("hsj"))
                                .add(Property.forName("xsmx.xsxt_xsdh").as("yyxsdh"))
                                .add(Property.forName("bhsj").as("bhsj"))
                                .add(Property.forName("xsmx.sjdj").as("sjdj"))
                                .add(Property.forName("xsmx.txx").as("txx"))
                                .add(Property.forName("xsmx.dlf").as("dlf"))
                                .add(Property.forName("xsmx.zjzyf").as("zjzyf"))
                                .add(Property.forName("xsmx.xkyh").as("xkyh"))
                                .add(Property.forName("xsmx.jsse").as("se"))
                                .add(Property.forName("xsmx.kpbhsj").as("kpbhsj"))
                                .add(Property.forName("xsmx.jsbhse").as("je"))
                                .add(Property.forName("xsmx.se").as("fpse"))
                                .add(Property.forName("swse").as("swse"))
                                .add(Property.forName("swje").as("swje"))
                                .add(Property.forName("xsjsd.ce").as("ce"))
                                .add(Property.forName("xsjsd.ddfy").as("ddfy"))
                ).add(Restrictions.eq("xsjsd.id", xsjsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        criteria.addOrder(Property.forName("wzgg.code").asc());
        return criteria.list();
    }
}
